﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionAutoEcoleModel;
using log4net;
using System.Data.Objects;
using GestionAutoEcole.DAO.Util;

namespace GestionAutoEcole.DAO.Impl
{
    public class ExaminateurDaoImpl : BaseRepository<examinateur>
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ExaminateurDaoImpl));

        private static volatile ExaminateurDaoImpl instance;
        private static object syncRoot = new Object();

        private ExaminateurDaoImpl() { }

        public static ExaminateurDaoImpl getInstance()
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new ExaminateurDaoImpl();
                }
            }

            return instance;
        }
        //création d'un groupe
        public override void Create(examinateur el)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin Create");
                }
                using (var context = new autoecolebase())
                {
                    // il faut plutot utiliser ça : context.VUGroupe.AddObject(grp);
                    context.examinateur.AddObject(el);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("Create : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End Create");
                }
            }
        }

        //modification
        public override void Update(examinateur entity)
        {

        }


        //Supprimer tous les groupe
        public override void DeleteAll()
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin DeleteAll");
                }
                using (var context = new autoecolebase())
                {
                    var examinateurList = context.examinateur;
                    foreach (var el in examinateurList)
                    {
                        context.DeleteObject(el);
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteAll : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End DeleteAll");
                }
            }
        }

        //Supprimer un groupe
        public override void Delete(examinateur el)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin Delete");
                }
                using (var context = new autoecolebase())
                {
                    var pers = context.examinateur.Where(u => u.EXAMINATEUR_ID == el.EXAMINATEUR_ID).First();
                    context.DeleteObject(pers);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("Delete : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End Delete");
                }
            }
        }

        //Récupérer tous les groupes
        public override List<examinateur> GetList()
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin GetList");
                }
                using (var context = new autoecolebase())
                {
                    return context.examinateur.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("GetList : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End GetList");
                }
            }
        }

        //Récupérer un groupe avec son Id
        public override examinateur FindById(int id)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin FindById");
                }
                using (var context = new autoecolebase())
                {
                    var grp = context.examinateur.Where(u => u.EXAMINATEUR_ID == id);
                    return grp.First();
                }
            }
            catch (Exception ex)
            {
                log.Error("FindById : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End FindById");
                }
            }
        }

        //Récupérer une liste d'groupes avec un critère de recherche
        public override List<examinateur> FindByCriteria(Object criteria)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin FindByCriteria");
                }
                using (var context = new autoecolebase())
                {

                    StringBuilder queryString =
                        new StringBuilder(@"SELECT VALUE grp FROM autoecolebase.examinateur as grp");
                    SelectBuilder sb = new SelectBuilder();

                    examinateur grp = (examinateur)criteria;
                    if (grp.EXAMINATEUR_NOM != null && !grp.EXAMINATEUR_NOM.Equals(""))
                    {
                        sb.AndSearchLike("grp.EXAMINATEUR_NOM", grp.EXAMINATEUR_NOM.ToString());
                    }

                    if (grp.EXAMINATEUR_PRENOM != null && !grp.EXAMINATEUR_PRENOM.Equals(""))
                    {
                        sb.AndSearchLike("grp.EXAMINATEUR_PRENOM", grp.EXAMINATEUR_PRENOM.ToString());
                    }

                    queryString.Append(sb.getQueryString());
                    ObjectQuery<examinateur> query = new ObjectQuery<examinateur>(queryString.ToString(), context);
                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("FindByCriteria : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End FindByCriteria");
                }
            }
        }

    }
}
